package com.sskj.hhrt;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.sskj.base.view.ProgressDialogs;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

public abstract class BaseActivity extends AppCompatActivity implements BaseView {

    private ProgressDialogs mProgressDialogs;

    private int mRequestCount;

    protected DisposableSubscriber<Long> disposableSubscriber;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().add(this);
        mProgressDialogs = new ProgressDialogs(this);
    }

    @Override
    public int color(int id) {
        return ContextCompat.getColor(this, id);
    }

    @Override
    public Drawable drawable(int id) {
        return ContextCompat.getDrawable(this, id);
    }

    @Override
    public void initRefreshLayout(SmartRefreshLayout refreshLayout, boolean isFooter) {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        if (isFooter) {
            refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        }
    }

    @Override
    public void initRefreshLayout(SmartRefreshLayout refreshLayout) {
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
    }

    @Override
    public void setText(TextView textView, String text) {
        if (textView != null&&text!=null) {
            textView.setText(text);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public boolean isEmpty(TextView textView) {
        return textView == null || TextUtils.isEmpty(textView.getText());
    }

    public String getText(TextView textView) {
        if (textView != null) {
            return textView.getText().toString();
        }
        return "";
    }

    public void startTimeDown(TextView getCodeView) {
        getCodeView.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                if (getCodeView != null) {
                    getCodeView.setText(time + "s后重试");
                }
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                if (getCodeView != null) {
                    getCodeView.setText(getString(com.sskj.base.R.string.get_code));
                    getCodeView.setEnabled(true);
                }
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void showLoading() {
        if (mProgressDialogs != null && mRequestCount <= 0) {
            mRequestCount = 1;
            mProgressDialogs.showDialog();
        } else {
            mRequestCount++;
        }
    }

    @Override
    public void hideLoading() {
        if (mRequestCount <= 1) {
            mRequestCount = 0;
            mProgressDialogs.closeDialog();
        } else {
            mRequestCount--;
        }
    }

    @Override
    protected void onDestroy() {
        AppManager.getInstance().remove(this);
        if (disposableSubscriber != null) {
            disposableSubscriber.dispose();
        }
        super.onDestroy();
    }
}
